import { ProductDetailInfoDTO } from './../../../../shared/service-proxy/service-proxies';
import { Component, OnInit, Injector, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from '@angular/forms';
@Component({
  selector: 'app-add-product-detail-modal',
  templateUrl: './add-product-detail-modal.component.html',
  styleUrls: ['./add-product-detail-modal.component.less']
})
export class AddProductDetailModalComponent extends AppComponentBase implements OnInit {
  isVisible: boolean = false;
  isLoading: boolean = false;
  isEdit: boolean = false;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  detail: ProductDetailInfoDTO = new ProductDetailInfoDTO();
  validateForm: FormGroup;
  constructor(
    injector: Injector,
    private fb: FormBuilder,
    private _service: ServiceProxy) {
    super(injector);
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      hsCode: [null, [Validators.required, Validators.maxLength(255)]],
      productName: [null, [Validators.required, Validators.maxLength(255)]],
      spc: [null, [Validators.required, Validators.maxLength(255)]],
      quantity: [null, [Validators.required]],
      origin: [null, [Validators.required, Validators.maxLength(255)]],
      destination: [null, [Validators.required, Validators.maxLength(255)]],
      price: [null, [Validators.required]],
      total: [null, [Validators.required]],
      moeda: [null, [Validators.required, Validators.maxLength(255)]],
      unidade: [null, [Validators.required, Validators.maxLength(255)]]
    });
  }

  show(pid?: string, detail?: ProductDetailInfoDTO, hsCode?: string): void {
    this.detail = new ProductDetailInfoDTO();
    if (!detail) {
      this.detail.infoId = pid;
      this.detail.hsCode = hsCode;
    }
    else {
      this.isEdit = true;
      this.detail.infoId = detail.infoId;
      this.detail.moeda = detail.moeda;
      this.detail.origin = detail.origin;
      this.detail.price = detail.price;
      this.detail.id = detail.id;
      this.detail.productName = detail.productName;
      this.detail.quantity = detail.quantity;
      this.detail.spc = detail.spc;
      this.detail.total = detail.total;
      this.detail.unidade = detail.unidade;
      this.detail.hsCode = detail.hsCode;
      this.detail.destination = detail.hsCode;
    }
    this.isVisible = true;
  }


  save(): void {
    this.isLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.valid) {
      this.message.success(this.l('save_success'));
      if (!this.isEdit) {
        this.modalSave.emit(this.detail);
      }
      else {
        this.modalSave.emit(this.detail);
      }
      this.validateForm.reset();
      this.close();
    }
    this.isLoading = false;
  }

  close(): void {
    this.isVisible = false;
  }

}
